package com.tian.config;

import com.tian.constant.RabbitMQConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

/**
 * {@code @description:} 创建direct类型的交换机
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2024-03-24 10:20
 * {@code @version:} 1.0
 */
@Slf4j
@Configuration
public class RabbitMqConfig {
    @Bean
    public Queue queueMessageInviteUserRegister() {
        return new Queue(RabbitMQConstant.INVITE_USER_REGISTER_QUEUE);
    }

    @Bean
    TopicExchange exchangeInviteUserRegister() {
        return new TopicExchange(RabbitMQConstant.INVITE_USER_REGISTER_EXCHANGE, true, false);
    }

    @Bean
    Binding bindingExchangeMessageInviteUserRegister(Queue queueMessageInviteUserRegister, TopicExchange exchangeInviteUserRegister) {
        return BindingBuilder.bind(queueMessageInviteUserRegister).to(exchangeInviteUserRegister).with(RabbitMQConstant.INVITE_USER_REGISTER_ROUTING_KEY);
    }

    @Bean
    public Queue queueUserUsedBalanceIncrease() {
        return new Queue(RabbitMQConstant.USER_BALANCE_INCREASE_QUEUE, true);
    }

    @Bean
    TopicExchange exchangeUsedBalanceIncrease() {
        return new TopicExchange(RabbitMQConstant.USER_BALANCE_INCREASE_EXCHANGE, true, false);
    }

    @Bean
    Binding bindingUsedBalanceIncrease(Queue queueUserUsedBalanceIncrease, TopicExchange exchangeUsedBalanceIncrease) {
        return BindingBuilder.bind(queueUserUsedBalanceIncrease).to(exchangeUsedBalanceIncrease).with(RabbitMQConstant.USER_BALANCE_INCREASE_ROUTING_KEY);
    }

    //延迟删除缓存
    @Bean
    public Queue queueDelayDeleteCache() {
        return new Queue(RabbitMQConstant.DELAY_DELETE_CACHE_QUEUE, true);
    }

    @Bean
    TopicExchange exchangeDelayDeleteCache() {
        return new TopicExchange(RabbitMQConstant.DELAY_DELETE_CACHE_EXCHANGE, true, false);
    }

    @Bean
    Binding bindingDelayDeleteCache(Queue queueDelayDeleteCache, TopicExchange exchangeDelayDeleteCache) {
        return BindingBuilder.bind(queueDelayDeleteCache).to(exchangeDelayDeleteCache).with(RabbitMQConstant.DELAY_DELETE_CACHE_ROUTING_KEY);
    }

    @Scope("prototype")
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate();
        rabbitTemplate.setConnectionFactory(connectionFactory);
        //设置消息投递失败的策略，有两种策略：自动删除或返回到客户端。
        //我们既然要做可靠性，当然是设置为返回到客户端(true是返回客户端，false是自动删除)
        rabbitTemplate.setMandatory(true);
        return rabbitTemplate;
    }
}

